var myDate = new Date(); //创建Date对象
var Y: any = myDate.getFullYear(); //获取当前完整年份
var M: any = myDate.getMonth() + 1; //获取当前月份
var D: any = myDate.getDate(); //获取当前日1-31
var H: any = myDate.getHours(); //获取当前小时
var i: any = myDate.getMinutes(); //获取当前分钟
var s: any = myDate.getSeconds(); //获取当前秒数
// 月份不足10补0
if (M < 10) {
	M = '0' + M;
}
// 日不足10补0
if (D < 10) {
	D = '0' + D;
}
// 小时不足10补0
if (H < 10) {
	H = '0' + H;
}
// 分钟不足10补0
if (i < 10) {
	i = '0' + i;
}
// 秒数不足10补0
if (s < 10) {
	s = '0' + s;
}
// 拼接日期分隔符根据自己的需要来修改
var currentTime: any = Y + '-' + M + '-' + D + ' ' + H + ':' + i + ':' + s;
var currentDay: any = Y + '-' + M + '-' + D;

function formatTime(time: any) {
	if (!time) {
		return '';
	}
	var date = new Date(time); // 初始化日期
	var year = date.getFullYear(); //获取年份
	var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1; // 获取月份
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); // 获取具体日
	var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); // 获取时
	var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); // 获取分
	var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); // 获取秒
	var currentDay = year + '-' + month + '-' + day;
	return {
		currentDay,
		year,
		month,
		day,
	};
}

//获取指定的年份
const lastYear = (type: any, index: any) => {
	let dateTime = new Date().getFullYear(); /* 获取现在的年份 */
	if (type === 'left') {
		/* 前一年 */
		dateTime = new Date(new Date().setFullYear(dateTime - index)).getFullYear();
	}
	if (type === 'right') {
		/* 后一年 */
		dateTime = new Date(new Date().setFullYear(dateTime + index)).getFullYear();
	}
	return dateTime;
};

const checkDateList = () => {
	const year1 = lastYear('left', 1);
	const year2 = lastYear('left', 2);
	const year3 = lastYear('left', 3);
	const year4 = lastYear('left', 4);
	const year5 = lastYear('left', 5);

	const shortcuts = [
		{
			text: '今年',
			value: () => {
				const end = new Date();
				const start = new Date(new Date().getFullYear(), 0);
				return [start, end];
			},
		},
		{
			value: () => {
				return [`${year1}-01-01`, `${year1}-12-31`];
			},
			text: year1,
		},
		{
			value: () => {
				return [`${year2}-01-01`, `${year2}-12-31`];
			},
			text: year2,
		},
		{
			value: () => {
				return [`${year3}-01-01`, `${year3}-12-31`];
			},
			text: year3,
		},
		{
			value: () => {
				return [`${year4}-01-01`, `${year4}-12-31`];
			},
			text: year4,
		},
		{
			value: () => {
				return [`${year5}-01-01`, `${year5}-12-31`];
			},
			text: year5,
		},
	];
	getCurrentMounth(shortcuts);
	return shortcuts;
};

function getCurrentMounth(shortcuts: any) {
	let ct = new Date();
	let res = ct.getMonth() + 1; //获取当前月份
	const year0 = lastYear('left', 0);

	for (let i = 1; i <= res; i++) {
		if (i == 3 || i == 6 || i == 9 || i == 12) {
			shortcuts.push({
				value: () => {
					if (i == 12) {
						return [`${year0}-${i - 2}-01`, new Date()];
					} else if (i == 3) {
						return [`${year0}-${i - 2}-01`, `${year0}-0${i}-31`];
					} else {
						return [`${year0}-${i - 2}-01`, `${year0}-0${i}-30`];
					}
				},
				text: `今年Q${i / 3}季度`,
			});
		}
	}
	//补充如果未到12月份设置季度
	if (res < 3) {
		shortcuts.push({
			value: () => {
				return [`${year0}-01-01`, new Date()];
			},
			text: `今年Q1季度`,
		});
	} else if (res < 6) {
		shortcuts.push({
			value: () => {
				return [`${year0}-06-01`, new Date()];
			},
			text: `今年Q2季度`,
		});
	} else if (res < 9) {
		shortcuts.push({
			value: () => {
				return [`${year0}-09-01`, new Date()];
			},
			text: `今年Q3季度`,
		});
	} else if (res < 12) {
		shortcuts.push({
			value: () => {
				return [`${year0}-12-01`, new Date()];
			},
			text: `今年Q4季度`,
		});
	}

	for (let i = 1; i <= res; i++) {
		if (res > i) {
			let lastDay = 0;
			if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
				lastDay = 31;
			} else if (i == 2) {
				lastDay = 28;
			} else {
				lastDay = 30;
			}
			shortcuts.push({
				value: () => {
					return [`${year0}-0${i}-01`, `${year0}-0${i}-${lastDay}`];
				},
				text: `今年${i}月`,
			});
		} else if (res == i) {
			shortcuts.push({
				value: () => {
					return [`${year0}-0${i}-01`, new Date()];
				},
				text: `今年${i}月`,
			});
		}
	}
}

//将 2023-05-10T08:58:33.000Z 格式化 2023-05-10 08:58:33

const NORMSTARTTIMEfilter = (date: any) => {
	// 转换后台返回时间格式
	const jsonDate = new Date(date).toJSON();

	// 加上8个小时
	const adjustedDate = new Date(new Date(jsonDate).getTime() + 8 * 3600 * 1000);

	// 格式化日期
	const formattedDate = adjustedDate
		.toISOString()
		.replace(/T/g, ' ')
		.replace(/\.[\d]{3}Z/, '');

	return formattedDate === '1970-01-01 00:00:00' ? '' : formattedDate;
};

//计算两个日期之间差几天
function getDaysDifference(oneDateStr: any, twoDateStr: any) {
	// var now = new Date();
	var oneDate = new Date(oneDateStr);
	var twoDate = new Date(twoDateStr);
	var diffInTime = oneDate.getTime() - twoDate.getTime();

	// 1 day = 24 hours
	//         = 24*60 minutes
	//         = 24*60*60 seconds
	//         = 24*60*60*1000 milliseconds
	var diffInDays = diffInTime / (1000 * 3600 * 24);
	return Math.floor(diffInDays) + 1; // 使用Math.abs来确保结果总是正数
}

export { getDaysDifference, currentTime, currentDay, formatTime, checkDateList, NORMSTARTTIMEfilter };
